package com.ibm.cognos.imt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;

@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/download", method=RequestMethod.GET)
    public void getFile(HttpServletResponse response) {
        System.out.println("now downloading");
        try {
            // get your file as InputStream
            InputStream is = new FileInputStream(new File("tweaked.xls"));
            // copy it to response's OutputStream
            org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @RequestMapping(value="/upload2", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload2(HttpServletRequest request,
                                                 HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;

        Iterator<String> itr = mRequest.getFileNames();
//        System.out.println(itr.next());
        while (itr.hasNext()) {
            MultipartFile mFile = mRequest.getFile(itr.next());
            String fileName = mFile.getOriginalFilename();
            System.out.println(fileName);
            byte[] bytes = mFile.getBytes();
            BufferedOutputStream stream =
                    new BufferedOutputStream(new FileOutputStream(new File("uploaded.xls")));
//            BufferedOutputStream stream =
//                    new BufferedOutputStream(new FileOutputStream(new File(fileName)));
            stream.write(bytes);
            stream.close();
        }
        return "You successfully uploaded";
    }


    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(
//            @RequestParam("name") String name,
            @RequestParam("file") MultipartFile data
    ) {
        if (!data.isEmpty()) {
            try {
                byte[] bytes = data.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File("template-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded";

            } catch (Exception e) {
                return "You failed to upload file  => " + e.getClass() + e.getMessage();
            }
        } else {
            return "You failed to upload because the file was empty.";
        }
    }



}